'use strict'

const { query } = require('./utils/model.js');
let axios = require('axios');
let { genToken } = require('./utils/token.js');
const mockData = require('./mock/movie.json');
let parseString = require('xml2js').parseString; // 引入xml解析模块
let js2xmlparser = require("js2xmlparser");
let md5 = require('md5') // 引入md5加密模块

const {
    getNonceStr,
    getOrder
} = require('./utils/tools.js');

const {
    appid,
    mch_id,
    appSecret,
    pay_api_key
} = require('./config.js');


// 定义控制器
var controller = {};

// 详情
controller.test = async (req, res) => {
    let {id} = req.query
    let {data} = await axios(`https://m.maoyan.com/api/mtrade/mmdb/movie/v5/${id}.json`);
    res.json(data)
} 

// 热映数据
controller.reyin = async (req, res) => {
    let {data} = await axios(`https://m.maoyan.com/ajax/movieOnInfoList`);
    res.json(data)
}

// 待映数据

controller.daiyin = async (req, res) => {
    let {ci,limit} = req.query
    let {data} = await axios(`https://i.maoyan.com/ajax/comingList?ci=${ci}&limit=${limit}&movieIds=&token`);
    res.json(data)
}

controller.wxlogin = async (req, res) => {
    let code = req.body.code 
    let wxAPI = `https://api.weixin.qq.com/sns/jscode2session?appid=wx462bc4b528a13c96&secret=8a232d3137a30edc2a4410673482c0c2&js_code=${code}&grant_type=authorization_code`
    let { data }  = await axios.get(wxAPI);
    let response = {};
    console.log(data)
    if (data.errcode) {
        response.errmsg = data.errmsg
    } else {
        let { openid,session_key } = data;
		let userInfo = {openid}
        let token = genToken(userInfo)
                response.token = token
                response.openid = openid
                response.errmsg = ''
       
    
    }
   
    res.json(response)
    
}


controller.login = async (req, res) => {
    let {username,password} = req.body
    if(username == "admin" && password == 123456){
        res.json({message:"登录成功",code:200})
    }else{
        res.json({message:"用户名或密码错误",code:400})
    }
}

module.exports = controller
